package apis

import (
	"fmt"
	"zinx/MMO_Game_Zinx/core"
	"zinx/MMO_Game_Zinx/pb"
	"zinx/Zinx/ziface"
	"zinx/Zinx/znet"

	"google.golang.org/protobuf/proto"
)

// 路由模块
// 世界聊天消息 msgID=2
type WorldChatApi struct {
	znet.BaseRouter
}

func (wc *WorldChatApi) Handle(request ziface.IRequest) {
	// 解析protobuf
	msg := request.GetData()
	proto_msg := &pb.Talk{}
	if err := proto.Unmarshal(msg, proto_msg); err != nil {
		fmt.Println("[WorldChatApi]Unmarshal err: ", err)
		return
	}
	// 获取消息发送者
	pid, err := request.GetConnection().GetProperty("pid")
	if err != nil {
		fmt.Println("[WorldChatApi]GetProperty err: ", err)
		return
	}
	player := core.WMObj.GetPlayerByPid(pid.(int32))
	// 广播消息 msgID=200 Tp=1 世界聊天
	// 发送消息给其他客户端
	player.BroadCastTalk(proto_msg.Content)
}
